<?php 

$titre = NULL;
$resume = NULL;
$contenu = NULL;
$image = NULL;
$idEdit = NULL;
$idCat = NULL;
$idStatus = NULL;

if(!empty($_GET['idArt']) && isset($_GET['action']) && "suppr"==$_GET['action']) //Deleting an article
{
	Article::delete($_GET['idArt']);
	redirige("index.php?page=list_articles.php");
}

if(!empty($_GET['idArt']) && isset($_GET['action']) && "edit"==$_GET['action']) //Loading article's datas
{
	$art = Article::read($_GET['idArt']);
	
	$titre = $art->getTitleArt();
	$resume = $art->getSummaryArt();
	$contenu = $art->getContentArt();
	$image = $art->getPathImageArt();
	$idCat = $art->getIdCategoryArt();
	$idStatus = $art->getIdStatusArt();
	$idEdit = $_GET['idArt'];	
}

if(isset($_POST['idEdit']) && null != $_POST['idEdit'])  //Editing an article
{
	$errors = array();
	
	if(empty($_POST['titre']))
		array_push($errors, "Titre obligatoire.");
		
	// Picture upload
	if(0 == count($errors))
	{
		$editArticle = Article::read($_POST['idEdit']);
		
		if(!$_FILES['image']['error'])
		{
			$extensions_valides = array( 'jpg' , 'jpeg' , 'gif' , 'png' );
			$extension_upload = strtolower(  substr(  strrchr($_FILES['image']['name'], '.')  ,1)  );
			if(! in_array($extension_upload,$extensions_valides)) 
				array_push($errors,"L'extension de l'image n'est pas valide");
			else
			{
				$name = md5(uniqid(rand(), true));
				$picName =$name.".".$extension_upload;
				$resultat = move_uploaded_file($_FILES['image']['tmp_name'],"./images/".$picName);
				if (!$resultat)
					array_push($errors, "Erreur d'upload.");
				else
				{
					if(resize("./images/".$picName,ARTICLE_PIC_WIDTH,ARTICLE_PIC_HEIGHT))
					{
						unlink("./".$editArticle->getPathImageArt());
						$editArticle->setPathImageArt($picName);
						generateRss(); //MAJ du fichier xml pour le flux RSS
					}
					else
						array_push($errors, "Erreur de redimensionnement de l'image.");
	
				}
			}
		}	
		
	}
	
	if(0 == count($errors))
	{	
		$editArticle->setTitleArt($_POST['titre']);		
		$editArticle->setSummaryArt(htmlspecialchars_decode($_POST['resume']));
		$editArticle->setContentArt(htmlspecialchars_decode($_POST['contenu']));
		$editArticle->setIdCategoryArt($_POST['categorie']);
		$editArticle->setIdStatusArt($_POST['statut']);
		$editArticle->save();
		generateRss(); //MAJ du fichier xml pour le flux RSS
		redirige("index.php?page=list_articles.php");
	}
}

if(isset($_POST['idEdit']) && null == $_POST['idEdit']) //Creating an article
{
	$errors = array();
	
	if(empty($_POST['titre']))
		array_push($errors, "Titre obligatoire.");
		
	if(empty($_POST['resume']))
		array_push($errors, "Resume obligatoire.");
		
	if(empty($_POST['contenu']))
		array_push($errors, "Contenu obligatoire.");
	
	if($_FILES['image']['error'])
		array_push($errors, "Image obligatoire.");
	else
	{
		$extensions_valides = array( 'jpg' , 'jpeg' , 'gif' , 'png' );
		$extension_upload = strtolower(  substr(  strrchr($_FILES['image']['name'], '.')  ,1)  );
		
		if(! in_array($extension_upload,$extensions_valides)) 
			array_push($errors,"L'extension de l'image n'est pas valide.");
	}
	
	if(0 == count($errors)) // Pas d'erreur, on contiue
	{
		$name = md5(uniqid(rand(), true));
		$picName =$name.".".$extension_upload;
		$resultat = move_uploaded_file($_FILES['image']['tmp_name'],"./images/".$picName);
		if (!$resultat)
			array_push($errors, "Erreur d'upload.");
		else
		{
			if(resize("./images/".$picName,ARTICLE_PIC_WIDTH,ARTICLE_PIC_HEIGHT))
			{
				$newArt = new Article($_POST['titre'],$_POST['resume'], $_POST['contenu'], $_SESSION['idUser'], $_POST['categorie'], $_POST['statut'],$picName);
				$newArt->save();
				generateRss(); //MAJ du fichier xml pour le flux RSS
			}
			else
				array_push($errors, "Erreur de redimensionnement de l'image.");
			
		}
	}	
}


$articlesList = Article::listing();
$mainCategoriesList = Category::listing("WHERE CAT_IdParent=0 ORDER BY CAT_Libelle");
$statusList = Status::listing("ORDER BY STT_Libelle");

include("partials/article.php");

?>

